package app.swap.enabler;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class activitySWAPEnabler extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn_on = (Button)findViewById(R.id.on);
        btn_on.setOnClickListener(swapon);
        Button btn_off = (Button)findViewById(R.id.off);
        btn_off.setOnClickListener(swapoff);
        validswap();
        validbutton();
    }
    
    private Button.OnClickListener swapon = new Button.OnClickListener(){
		public void onClick(View arg0) {
				if (validswap() == 1){
			    	validbutton();
					}
			    else{
			    	EditText swprt = (EditText)findViewById(R.id.swappart);
			    	int swp = Integer.parseInt(swprt.getText().toString());
			    	String on = "swapon /dev/block/mmcblk0p"+swp;
			    	Process p = null;
			    	try {
			    		p = Runtime.getRuntime().exec(new String[] {"su","-c", on});
			    		BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
			    		TextView error = (TextView)findViewById(R.id.error);
			    		error.setText(stdError.readLine());
			    		validswap();
			    		validbutton();
			    	} catch (IOException e) {
			    		e.printStackTrace();
			    	}
			}
		}
    };
    
    private Button.OnClickListener swapoff = new Button.OnClickListener(){
		public void onClick(View arg0) {
			if (validswap() == 0){
				validbutton();
			}
        	else{
        		EditText swprt = (EditText)findViewById(R.id.swappart);
        		int swp = Integer.parseInt(swprt.getText().toString());
        		String off = "swapoff /dev/block/mmcblk0p"+swp;
        		Process p = null;
        		try {
        			p = Runtime.getRuntime().exec(new String[] {"su","-c", off});
        			BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
        			TextView error = (TextView)findViewById(R.id.error);
        			error.setText(stdError.readLine());
        			validswap();
        			validbutton();
        		} catch (IOException e) {
				e.printStackTrace();
        		}
        }
	}
};
        
    public void validbutton(){
    	if (validswap() == 1){
	        Button btn_off = (Button)findViewById(R.id.off);
	        btn_off.setEnabled(true);
	        Button btn_on = (Button)findViewById(R.id.on);
	        btn_on.setEnabled(false);
    	}
	    else if(validswap() == 0){
		    Button btn_on = (Button)findViewById(R.id.on);
		    btn_on.setEnabled(true);
		    Button btn_off = (Button)findViewById(R.id.off);
		    btn_off.setEnabled(false);
	    }
    }
    
    public int validswap(){
        Process p = null;
		try {
			p = Runtime.getRuntime().exec("grep / /proc/swaps");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
        BufferedReader stdInput = new BufferedReader(new 
        InputStreamReader(p.getInputStream()));
        TextView status = (TextView)findViewById(R.id.status);
		try {
			if  (stdInput.readLine() == null){
				status.setText("SWAP turn off");
				return 0;
			}
			else{
				status.setText("SWAP turn on");
				return 1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
    }
}
